package com.xxxy.jsq.action.teacher;  // 保持与其他教师相关Servlet一致的包路径

import com.xxxy.jsq.dao.TeacherDao;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 教师删除功能Servlet
 */
@WebServlet("/deleteTeacherServlet")  // 映射路径，与部门删除保持命名风格一致
public class deleteTeacherServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public deleteTeacherServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置字符编码，避免中文乱码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        // 创建教师相关的DAO对象
        TeacherDao dao = new TeacherDao();

        // 获取页面传递的教师ID参数（与前端删除按钮传递的参数名保持一致）
        Long teacherId = Long.valueOf(request.getParameter("cid"));  // 沿用部门删除的参数名"cid"，确保前后端一致

        // 调用DAO层的删除方法（根据ID删除）
        String result = dao.deleteById(teacherId);

        // 将删除结果返回给前端
        PrintWriter out = response.getWriter();
        out.print(result);  // 返回"Ok"表示成功，"Err"表示失败，或具体错误信息
        out.flush();
        out.close();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //  POST请求也交给doGet处理，保持与部门删除的处理方式一致
        doGet(request, response);
    }
}